#!/sbin/openrc-run

name=mongooseim
description="MongooseIM XMPP server"

depend() {
	use dns
	need net
	after firewall
	provide jabber-server
}

start_pre() {
	checkpath -d -m 0750 -o "mongooseim:mongooseim" \
		/usr/lib/mongooseim/log /usr/lib/mongooseim/priv \
		/etc/mongooseim /var/lib/mongooseim /var/lock/mongooseim \
		/var/log/mongooseim
}

start() {
	if mongooseimctl status >/dev/null 2>&1; then
		ewarn "MongooseIM is already started."
		return 0
	fi
	ebegin "Starting MongooseIM"
	/usr/bin/mongooseimctl start
	eend $?
}

stop() {
	mongooseimctl status >/dev/null 2>&1
	if test $? = 3; then
		ewarn "MongooseIM is already stopped."
		return 0
	fi
	ebegin "Stopping MongooseIM"
	if /usr/bin/mongooseimctl stop >/dev/null 2>&1; then
		cnt=0
		sleep 1
		while mongooseimctl status >/dev/null 2>&1 || test $? = 1; do
			echo -n .
			cnt=`expr $cnt + 1`
			if [ $cnt -ge 60 ] ; then
				eend 1
				break
			fi
			sleep 1
		done
		eend 0
	else
		eend 1
		einfo "Please run '/usr/bin/mongooseimctl status' to see what's going on."
	fi
	eend 0
}
